package com.dhsc.service.impl.yykh.yyzlkh;

import com.dhsc.domain.ywzx.yykh.yyzlkh.YwxtcsVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.yykh.yyzlkh.IYwxtcsDao;
import com.dhsc.service.yykh.yyzlkh.IYwxtcsService;
import com.dhsc.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName YwxtcsServiceImpl
 * @description: 业务系统测试
 * @author: gx
 * @create: 2024-04-25 18:47:10
 **/
@Service
@Slf4j
public class YwxtcsServiceImpl implements IYwxtcsService {

    @Autowired
    private IYwxtcsDao ywxtcsDao;

    @Override
    public LayuiTableVo queryYwxtcs(YwxtcsVo ywxtcsVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            List<YwxtcsVo> ywxtcsVos = ywxtcsDao.queryYwxtcs(ywxtcsVo);
            //需要按照bmqx分组处理
            List<YwxtcsVo> list = new ArrayList<>();
            for (int i = 0; i < ywxtcsVos.size(); i++) {
                if (i == 0 || (i > 0 && !ywxtcsVos.get(i).getBmqx().equals(ywxtcsVos.get(i-1).getBmqx()))){
                    YwxtcsVo ywxtcs = new YwxtcsVo();
                    ywxtcs.setJgdm(ywxtcsVos.get(i).getBmqxmc() + "档");
                    ywxtcs.setState(1);
                    list.add(ywxtcs);
                }
                list.add(ywxtcsVos.get(i));
            }
            obj.setMsg("");
            obj.setData(list);
            obj.setCode(0);
            obj.setCount(ywxtcsVos.size());
        }catch (Exception e){
            log.error("查询业务系统测试信息出错",e);
            throw new MyException("查询业务系统测试信息出错");
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject saveYwxtcs(YwxtcsVo ywxtcsVo) {
        ResponseObject obj = new ResponseObject();
        try{
            //先清除掉表内信息
            ywxtcsDao.removeYwxtcs();
            List<YwxtcsVo> list = CommonUtil.readValueAsListIgnoreUnknown(ywxtcsVo.getJson(), YwxtcsVo.class);
            ywxtcsDao.saveYwxtcs(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("保存导入的业务系统测试信息出错", e);
            throw new MyException("保存导入的业务系统测试信息出错");
        }
        return obj;
    }
}
